<template>
  <div class="in-wrap">
    <!-- 公共头引入 -->
    <header id="header">
      <section class="container">
        <h1 id="logo">
          <a href="#" title="枫云教育">
            <img src="https://read-picture-001.oss-cn-beijing.aliyuncs.com/logo.png" alt="logo">
          </a>
        </h1>
        <div class="h-r-nsl">
          <ul class="nav">
            <router-link to="/" tag="li" active-class="current" exact>
              <a>首页</a>
            </router-link>
            <router-link to="/course" tag="li" active-class="current">
              <a>课程</a>
            </router-link>
            <router-link to="/teacher" tag="li" active-class="current">
              <a>师资</a>
            </router-link>
            <router-link to="/article" tag="li" active-class="current">
              <!-- <a>文章</a> -->
              <a>云记</a>
            </router-link>
            <router-link to="/forum" tag="li" active-class="current">
              <!-- <a>问答</a> -->
              <a>论坛</a>
            </router-link>
          </ul>
          <!-- / nav -->
          <ul class="h-r-login">
              <li v-if="!loginInfo.id" id="no-login">
                <a href="/login" title="登录">
                  <em class="icon18 login-icon">&nbsp;</em>
                  <span class="vam ml5">登录</span>
                </a>
                <a href="/register" title="注册">
                  <span class="vam ml5">注册</span>
                </a>
              </li>
              <li v-if="loginInfo.id" id="is-login-two" class="h-r-user">
                <el-dropdown @command="handleCommand">
                  <!--  可以跳转到 /ucenter，这个界面可以用来展示个人主页 <a href="/ucenter" title> --> 
                  <a title>
                    <img
                      :src="loginInfo.avatar"
                      width="30"
                      height="30"
                      class="vam picImg"
                      alt
                    >
                    <span id="userName" class="vam disIb" :class="[{'teacher_identification': Number(loginInfo.sign) === 1}]">{{ loginInfo.nickname }}</span>
                  </a>
                  <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item command="user_center">个人中心</el-dropdown-item>
                      <el-dropdown-item command="user_favorites">收藏夹</el-dropdown-item>
                      <el-dropdown-item command="user_unrememberWord">退出登录</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </li>
              <!-- /未登录显示第1 li；登录后显示第2，3 li -->
          </ul>
          <aside class="h-r-search">
            <form action="#" method="post">
              <label class="h-r-s-box">
                <!-- <input type="text" placeholder="输入你想学的课程" name="queryCourse.courseName" value>
                <button type="submit" class="s-btn">
                  <em class="icon18">&nbsp;</em>
                </button> -->
              </label>
            </form>
          </aside>
        </div>
        <aside class="mw-nav-btn">
          <div class="mw-nav-icon"></div>
        </aside>
        <div class="clear"></div>
      </section>
    </header>
    <!-- /公共头引入 -->
    <div class="main-wrap">
      <nuxt/>
    </div>
  </div>
</template>
<script>
import '~/assets/css/reset.css'
import '~/assets/css/theme.css'
import '~/assets/css/global.css'
import '~/assets/css/web.css'
import '~/assets/css/base.css'
import '~/assets/css/activity_tab.css'
import '~/assets/css/bottom_rec.css'
import '~/assets/css/nice_select.css'
import '~/assets/css/order.css'
import '~/assets/css/swiper-3.3.1.min.css'
import "~/assets/css/pages-weixinpay.css"

import loginApi from '@/api/login'
import cookie from 'js-cookie'
import studyApi from '@/api/study'

export default {
  data() {
    return {
      token:'',
      loginInfo: {
        id: '',
        age: '',
        avatar: '',
        mobile: '',
        nickname: '',
        sex: ''
      },
      timeout: null,

      fromOptions: [{
          value: 'en',
          label: '英文'
        },
        {
          value: 'zh',
          label: '中文'
        },],
      from: 'en',

      toOptions: [{
          value: 'zh',
          label: '中文'
        },
        {
          value: 'en',
          label: '英文'
        },],
      to: 'zh',
      translateSentence: {},

      word_name: '',
      word: {
          audio: '',
          explanation: '',
          addOperation: [],
      }

    }
  },
  watch: {
    translate_content(curVal, oldVal) {
        // 实现input连续输入，只发一次请求
        clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
          this.translateContent();
        }, 600);
    },
    word_name(curVal, oldVal) {
      // 实现input连续输入，只发一次请求
        clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
          this.wordDictionary();
        }, 800);
    }
  },
  created() {
    //判断路径是否包含token参数
    this.token = this.$route.query.token
    // console.log('判断路径是否包含token参数')
    // console.log(this.token)
    if (this.token) {
      this.wxLoging()
    } else {
      this.getInfo()
    }
  },
  methods: {
    //用户头像位置下拉菜单点击事件
    handleCommand(command) {
         if (command === 'user_center') {//进入个人中心
            this.$router.push({path:'/userCenter/'})
         }
         if (command === 'user_favorites') {
            this.$router.push({path:'/favorites/'})
         }
         if (command === 'user_unrememberWord') {
          cookie.set("token", '', {domain:'localhost'})
          cookie.set("ucenter", '', {domain:'localhost'})
          //跳回首页，类似页面刷新
          window.location.href="/"
         } 
    },

    wxLoging() {
      //将token放到cookie里，让请求头携带token发送请求
      cookie.set("token", this.token, {domain:'localhost'})
      //利用token得到用户信息
      loginApi.getMemberInfo()
            .then(response => {
              this.loginInfo = response.data.data.userInfo
              //把用户信息保存到cookie里
              //注意这里一定要将JSON对象转为字符串再存储！！！
              cookie.set("ucenter", JSON.stringify(this.loginInfo), {domain:'localhost'})
            })
    },
    getInfo() {
      //从cookie获取用户信息
      var userStr = cookie.get('ucenter')
      // 把字符串转换json对象(js对象)
      if(userStr) {
        this.loginInfo = JSON.parse(userStr)
      }
    },
  },
};
</script>

<style>
.nav a{
  font-weight: bold;
}
#userName{
  font-weight: bold;
  width: auto;
  max-width: 100px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  vertical-align: middle;
}
/* .main-wrap{
  background-color: #e4e6eb !important;

} */



</style>
